#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:MrG
from django import forms
from django.core.validators import RegexValidator

from web import models


class RegisterModeForm(forms.ModelForm):
    """
    自动生成标签的类
    """
    mobile_phone = forms.CharField(label='手机号', validators=[RegexValidator(r'^(1[3|4|5|6|7|8|9])\d{9}$', '手机号格式错误'), ])
    password = forms.CharField(label='密码', widget=forms.PasswordInput(attrs={'class': 'form-control'}))
    confirm_password = forms.CharField(label='重复密码', widget=forms.PasswordInput)
    code = forms.CharField(label='验证码')

    class Meta:
        model = models.UserInfo
        # fields = '__all__'
        fields = ['username', 'email', 'password', 'confirm_password', 'mobile_phone', 'code']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # self.fields拿到model的所有字段
        # 每个字段都加一个class
        for name, field in self.fields.items():
            field.widget.attrs['class'] = 'form-control'
            field.widget.attrs['placeholder'] = '请输入%s' % field.label
